import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Params} from '@angular/router';

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {

  private pid: number;  

  private name: string;

  constructor(private routerInfo: ActivatedRoute) { }

  ngOnInit() {
    // this.pid = this.routerInfo.snapshot.params["id"];      // 路径中含参数  {path: 'pd/:id', component: ProductComponent},
    this.routerInfo.params.subscribe((params: Params) => this.pid = params['id']); // 参数订阅  参数在变化的

    this.routerInfo.data.subscribe((data: {product: Product}) => {
      console.log(this.routerInfo);
      console.log(data);
      this.pid = data.product.id;
      this.name = data.product.name;
    })

  }

}

export class Product {

  constructor(public id:number, public name: string) {}

}
